﻿using BlogDemo.Application;
using BlogDemo.Application.BlogApp;
using BlogDemo.Application.BlogApp.Dtos;
using BlogDemo.Core.Repository;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace BlogDemo.Api.Controllers
{
    [Route("api/blog")]
    [ApiController]
    public class BlogController : ControllerBase
    {
        private readonly IBlogRepostory _blogRepository;
        private readonly IBlogAppService blogAppService;
        private readonly IUrlHelper _urlHelper;
        public BlogController(IBlogAppService blogAppService, IUrlHelper urlHelper)
        {
            this.blogAppService = blogAppService;
            this._urlHelper = urlHelper;
        }

        [HttpGet]
        public ActionResult<IEnumerable<BlogDto>> GetBlogs()
        {
            return Ok(this.blogAppService.GetAll());
        }
        [HttpGet]
        [Route("{id}", Name = "GetBlog")]
        public async Task<IActionResult> Get(int id)
        {
            var blogVm = this.blogAppService.Get(id);
            if (blogVm == null)
            {
                return NotFound();
            }
            //var blogVm = Mapper.Map<BlogViewModel>(entity);
            return Ok(CreateLinksForBlog(blogVm, id));
        }
        //[HttpPost]
        //public async Task<IActionResult> Post([FromBody] BlogDto vehicleVm)
        //{
        //    if (vehicleVm == null)
        //    {
        //        return BadRequest();
        //    }
        //    if (!ModelState.IsValid)
        //    {
        //        return BadRequest(ModelState);
        //    }
        //    if (!await UnitOfWork.SaveAsync())
        //    {
        //        return StatusCode(500, "保存时出错");
        //    }
        //    var vm = Mapper.Map<BlogDto>(newItem);
        //    return CreatedAtRoute("GetBlog", new { id = vm.Id }, CreateLinksForBlog(vm));
        //}
        //[HttpPut("{id}", Name = "UpdateBlog")]
        //public async Task<IActionResult> Put(int id, [FromBody] BlogDto blogVm)
        //{
        //    if (blogVm == null)
        //    {
        //        return BadRequest();
        //    }
        //    if (!ModelState.IsValid)
        //    {
        //        return BadRequest(ModelState);
        //    }
        //    var dbItem = await _blogRepository.Find(id);
        //    if (dbItem == null)
        //    {
        //        return NotFound();
        //    }
        //    Mapper.Map(blogVm, dbItem);
        //    _blogRepository.Update(dbItem);
        //    if (!await UnitOfWork.SaveAsync())
        //    {
        //        return StatusCode(500, "保存时出错");
        //    }
        //    return NoContent();
        //}
        //[HttpPatch("{id}", Name = "PartiallyUpdateBlog")]
        //public async Task<IActionResult> Patch(int id, [FromBody] JsonPatchDocument<BlogDto> patchDoc)
        //{
        //    if (patchDoc == null)
        //    {
        //        return BadRequest();
        //    }
        //    var entity = await _blogRepository.Find(id);
        //    if (entity == null)
        //    {
        //        return NotFound();
        //    }
        //    var toPatchVm = Mapper.Map<BlogDto>(entity);
        //    patchDoc.ApplyTo(toPatchVm, ModelState);
        //    TryValidateModel(toPatchVm);
        //    if (!ModelState.IsValid)
        //    {
        //        return BadRequest(ModelState);
        //    }
        //    Mapper.Map(toPatchVm, dbItem);
        //    if (!await UnitOfWork.SaveAsync())
        //    {
        //        return NoContent();
        //    }
        //}
        //[HttpDelete("{id}", Name = "DeleteVehicle")]
        //public async Task<IActionResult> Delete(int id)
        //{
        //    var model = await _blogRepository.Find(id);
        //    if (model == null)
        //    {
        //        return NotFound();
        //    }
        //    _blogRepository.Remove(model);
        //    if (!await UnitOfWork.SaveAsync())
        //    {
        //        return StatusCode(500, "删除时出错");
        //    }
        //    return NoContent();
        //}
        private BlogDto CreateLinksForBlog(BlogDto blogDto, int blogId)
        {
            var id = blogId;
            blogDto.Links.Add(new LinkDto(href: _urlHelper.Link("GetBlog", new { id = id }), rel: "self", method: "GET"));
            blogDto.Links.Add(new LinkDto(href: _urlHelper.Link("UpdateBlog", new { id = id }), rel: "update_blog", method: "PUT"));
            blogDto.Links.Add(new LinkDto(href: _urlHelper.Link("PartiallyUpdateBlog", new { id = id }), rel: "partially_update_blog", method: "PATCH"));
            blogDto.Links.Add(new LinkDto(href: _urlHelper.Link("DeleteBlog", new { id = id }), rel: "delete_blog", method: "DELETE"));
            return blogDto;
        }
    }
}